function toAddDep() {
    layer.open({
        type: 2,
        title: '添加部门信息',
        maxmin: true,
        shadeClose: true, //点击遮罩关闭层
        area: ['400px', '550px'],
        content: base_url + '/manage/toAddDep'
    });
}

function addDep() {
    var depNamediv = $('#depNamediv');
    var depDescdiv = $('#depDescdiv');
    var depNameMsg = $('#depNameMsg');
    var depDescMsg = $('#depDescMsg');

    depNamediv.removeClass("has-error");
    depDescdiv.removeClass("has-error");
    depNameMsg.html("");
    depDescMsg.html("");

    var depName = $('#depName').val();
    var depDesc = $('#depDesc').val();
    if (null == depName || depName == "") {
        depNamediv.addClass("has-error");
        depNameMsg.html("部门名不能为空！");
        return;
    }
    if (null == depDesc || depDesc == "") {
        depDescdiv.addClass("has-error");
        depDescMsg.html("部门名不能为空！");
        return;
    }
    var pid = $('#pid').val();
    if (null != pid && pid != "") {
        var pname = $('#pid').find("option:selected").text();
        $('#pname').val(pname);
    }

    var ii = layer.load();
    $.ajax({
        url: base_url + "/manage/addDep",
        type: "post",
        data: $('#addDepForm').serialize(),
        async: false,//设置同步方式，非异步！
        cache: false,//严格禁止缓存！
        success: function (data) {
            setTimeout(function () {
                layer.close(ii);
            }, 500);
            if ("Success" == data.code) {
                layer.confirm("添加成功！", {
                    icon: 6,
                    title: "系统提示",
                    btn: ["确定"]
                }, function (index) {
                    layer.close(index);
                    var index2 = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index2);
                    parent.window.location.href = base_url + "/manage/toDepartment";
                });
            } else if ("DepExist" == data.code) {
                $('#depNamediv').addClass("has-error");
                $('#depNameMsg').html(data.msg);
            } else {
                layer.alert("添加失败：" + data.msg, {icon: 5});
            }
        }
    });
}

function deleteDep(id) {
    var result;
    layer.confirm("确认删除", {
        icon: 3,
        title: "系统提示",
        btn: ["确定", "关闭"]
    }, function (index) {
        layer.close(index);
        $.ajax({
            url: base_url + "/manage/deleteDep?id=" + id,
            type: "get",
            async: false,//设置同步方式，非异步！
            cache: false,//严格禁止缓存！
            success: function (data) {
                result = data;
            }
        });
        if ("Success" == result.code) {
            layer.alert("删除成功！", {icon: 6});
            $("#" + id).remove();
        } else {
            layer.alert("删除失败：" + result.msg, {icon: 5});
        }
    });
}

function toUpdateDep(id) {
    layer.open({
        type: 2,
        title: '修改部门信息',
        maxmin: true,
        shadeClose: true, //点击遮罩关闭层
        area: ['400px', '550px'],
        content: base_url + '/manage/toUpdateDep?id=' + id
    });
}

function updateDep() {
    var depNamediv = $('#depNamediv');
    var depDescdiv = $('#depDescdiv');
    var depNameMsg = $('#depNameMsg');
    var depDescMsg = $('#depDescMsg');

    depNamediv.removeClass("has-error");
    depDescdiv.removeClass("has-error");
    depNameMsg.html("");
    depDescMsg.html("");

    var depName = $('#depName').val();
    var depDesc = $('#depDesc').val();
    if (null == depName || depName == "") {
        depNamediv.addClass("has-error");
        depNameMsg.html("部门名不能为空！");
        return;
    }
    if (null == depDesc || depDesc == "") {
        depDescdiv.addClass("has-error");
        depDescMsg.html("部门名不能为空！");
        return;
    }
    var pid = $('#pid').val();
    if (null != pid && pid != "") {
        var pname = $('#pid').find("option:selected").text();
        $('#pname').val(pname);
    }

    var ii = layer.load();
    $.ajax({
        url: base_url + "/manage/updateDep",
        type: "post",
        data: $('#updateDepForm').serialize(),
        async: false,//设置同步方式，非异步！
        cache: false,//严格禁止缓存！
        success: function (data) {
            setTimeout(function () {
                layer.close(ii);
            }, 500);
            if ("Success" == data.code) {
                layer.confirm("更新成功！", {
                    icon: 6,
                    title: "系统提示",
                    btn: ["确定"]
                }, function (index) {
                    layer.close(index);
                    var index2 = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index2);
                    parent.window.location.href = base_url + "/manage/toDepartment";
                });
            } else if ("DepExist" == data.code) {
                $('#depNamediv').addClass("has-error");
                $('#depNameMsg').html(data.msg);
            } else {
                layer.alert("更新失败：" + data.msg, {icon: 5});
            }
        }
    });
}

function toAddOpenApi() {
    layer.open({
        type: 2,
        title: '添加授权信息',
        maxmin: true,
        shadeClose: true, //点击遮罩关闭层
        area: ['500px', '550px'],
        content: base_url + '/manage/toAddOpenApi'
    });
}

function addOpenApi() {
    var appIddiv = $('#appIddiv');
    var appKeydiv = $('#appKeydiv');
    var appIdMsg = $('#appIdMsg');
    var appKeyMsg = $('#appKeyMsg');

    appIddiv.removeClass("has-error");
    appKeydiv.removeClass("has-error");
    appIdMsg.html("");
    appKeyMsg.html("");

    var appId = $('#appId').val();
    var appKey = $('#appKey').val();

    if (null == appId || appId == "") {
        appIddiv.addClass("has-error");
        appIdMsg.html("APP ID不能为空！");
        return;
    }
    if (null == appKey || appKey == "") {
        appKeydiv.addClass("has-error");
        appKeyMsg.html("秘钥串不能为空！");
        return;
    }
    var ii = layer.load();
    $.ajax({
        url: base_url + "/manage/addOpenApi",
        type: "post",
        data: $('#openApiForm').serialize(),
        async: false,//设置同步方式，非异步！
        cache: false,//严格禁止缓存！
        success: function (data) {
            setTimeout(function () {
                layer.close(ii);
            }, 500);
            if ("Success" == data.code) {
                layer.confirm("添加成功！", {
                    icon: 6,
                    title: "系统提示",
                    btn: ["确定"]
                }, function (index) {
                    layer.close(index);
                    var index2 = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index2);
                    parent.window.location.href = base_url + "/manage/toOpenApi";
                });
            } else if ("AppExist" == data.code) {
                $('#appIddiv').addClass("has-error");
                $('#appIdMsg').html(data.msg);
            } else {
                layer.alert("添加失败：" + data.msg, {icon: 5});
            }
        }
    });
}

function updateOpenApi() {
    var appIddiv = $('#appIddiv');
    var appNamediv = $('#appNamediv');
    var appKeydiv = $('#appKeydiv');
    var appIdMsg = $('#appIdMsg');
    var appNameMsg = $('#appNameMsg');
    var appKeyMsg = $('#appKeyMsg');

    appIddiv.removeClass("has-error");
    appNamediv.removeClass("has-error");
    appKeydiv.removeClass("has-error");
    appIdMsg.html("");
    appNameMsg.html("");
    appKeyMsg.html("");

    var appId = $('#appId').val();
    var appName = $('#appName').val();
    var appKey = $('#appKey').val();

    if (null == appId || appId == "") {
        appIddiv.addClass("has-error");
        appIdMsg.html("APP ID不能为空！");
        return;
    }
    if (null == appName || appName == "") {
        appNamediv.addClass("has-error");
        appNameMsg.html("应用名不能为空！");
        return;
    }
    if (null == appKey || appKey == "") {
        appKeydiv.addClass("has-error");
        appKeyMsg.html("秘钥串不能为空！");
        return;
    }
    var ii = layer.load();
    $.ajax({
        url: base_url + "/manage/updateApi",
        type: "post",
        data: $('#updateApiForm').serialize(),
        async: false,//设置同步方式，非异步！
        cache: false,//严格禁止缓存！
        success: function (data) {
            setTimeout(function () {
                layer.close(ii);
            }, 500);
            if ("Success" == data.code) {
                layer.confirm("修改成功！", {
                    icon: 6,
                    title: "系统提示",
                    btn: ["确定"]
                }, function (index) {
                    layer.close(index);
                    var index2 = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index2);
                    parent.window.location.href = base_url + "/manage/toOpenApi";
                });
            } else if ("AppExist" == data.code) {
                $('#appIddiv').addClass("has-error");
                $('#appIdMsg').html(data.msg);
            } else {
                layer.alert("修改失败：" + data.msg, {icon: 5});
            }
        }
    });
}

function createKey() {
    var ii = layer.load();
    $.ajax({
        url: base_url + "/manage/createKey",
        type: "get",
        async: false,//设置同步方式，非异步！
        cache: false,//严格禁止缓存！
        success: function (data) {
            setTimeout(function () {
                layer.close(ii);
            }, 500);
            if ("Success" == data.code) {
                $('#appKey').val(data.key);
            } else {
                layer.alert("获取失败：" + data.msg, {icon: 5});
            }
        }
    });
}

function showApiKey(id) {

}

function toUpdateApi(id) {
    layer.open({
        type: 2,
        title: '修改授权信息',
        maxmin: true,
        shadeClose: true, //点击遮罩关闭层
        area: ['500px', '550px'],
        content: base_url + '/manage/toUpdateApi?id=' + id
    });
}

function deleteApi(id) {
    var result;
    layer.confirm("确认删除", {
        icon: 3,
        title: "系统提示",
        btn: ["确定", "关闭"]
    }, function (index) {
        layer.close(index);
        $.ajax({
            url: base_url + "/manage/deleteApi?id=" + id,
            type: "get",
            async: false,//设置同步方式，非异步！
            cache: false,//严格禁止缓存！
            success: function (data) {
                result = data;
            }
        });
        if ("Success" == result.code) {
            layer.alert("删除成功！", {icon: 6});
            $("#" + id).remove();
        } else {
            layer.alert("删除失败：" + result.msg, {icon: 5});
        }
    });
}

function changeStatus(id, obj) {
    var value = $(obj).text();
    var type;
    if (value == "启用") {
        type = "Start";
    } else {
        type = "Stop";
    }
    layer.confirm("是否" + value, {
        icon: 3,
        title: "系统提示",
        btn: ["确定", "关闭"]
    }, function (index) {
        layer.close(index);
        $.ajax({
            url: base_url + "/manage/changeApiStatus",
            type: "post",
            data: {"id": id, "status": type},
            success: function (data) {
                if ("Success" != data.code) {
                    layer.alert("项目 " + type + " 失败！", {icon: 5});
                } else {
                    var td = $(obj).parents('tr').children('td');
                    if ("Start" == type) {
                        td.eq(2).text("启用");
                        $(obj).text("停用");
                        $(obj).removeClass("btn-primary");
                        $(obj).addClass("btn-danger");
                    } else {
                        td.eq(2).text("停用");
                        $(obj).text("启用");
                        $(obj).removeClass("btn-danger");
                        $(obj).addClass("btn-primary");
                    }
                }
            }
        });
    });
}

function refresh() {
    layer.confirm("是否刷新所有应用缓存", {
        icon: 3,
        title: "系统提示",
        btn: ["确定", "关闭"]
    }, function (index) {
        layer.close(index);
        var ii = layer.load();
        $.ajax({
            url: base_url + "/manage/refresh",
            type: "get",
            success: function (data) {
                setTimeout(function () {
                    layer.close(ii);
                }, 500);
                if ("Success" != data.code) {
                    layer.alert("刷新失败：" + data.msg, {icon: 5});
                } else {
                    layer.confirm("刷新成功！", {
                        icon: 6,
                        title: "系统提示",
                        btn: ["确定"]
                    }, function (index) {
                        layer.close(index);
                        var index2 = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index2);
                    });
                }
            }
        });
    });
}


